import { AnyAction, Reducer } from 'redux';
import { EffectsCommandMap } from 'dva';
import { message } from 'antd';
import { fetchValidation } from '@/utils';
import {
  queryMouthDetailsList,
  queryCellTypeInfo,
  queryStatusInfo,
  operCheckCellOrder,
  operUpdateMouth,
  operUpdateDesc,
  operActionOpenCell,
  queryDetailInfo,
  operactionSeting,
  queryCollectionLastInfo,
  queryUpdateCellIdle
} from './service';

export type Effect = (
  action: AnyAction,
  effects: EffectsCommandMap & { select: <T>(func: (state: {}) => T) => T },
) => void;

export interface ModelType {
  namespace: string;
  state: {};
  effects: {
    fetch: Effect;
    fetchCellType: Effect;
    fetchStatus: Effect;
    fetchDetail: Effect;
    fetchLastInfo: Effect;
    checkMouth: Effect;
    setMouth: Effect;
    changeDesc: Effect;
    openCell: Effect;
    addOrUpdateSet: Effect;
    updateCellIdle:Effect;
  };
  reducers: {
    save: Reducer;
    orderStatus: Reducer;
    saveCellType: Reducer;
    saveState: Reducer;
    saveDetail: Reducer;
    saveLastInfo: Reducer;
  };
}

const Model: ModelType = {
  namespace: 'mouthDetailsList',

  state: {
    dataInfo: {
      cellCount: [],
      list: [],
      count: 0,
    },
    detailInfo: {},
    orderVisible: false,
    cellTypeList: [],
    statusList: [],
    unfinishedOrder: '',
    lastInfo: {},
  },

  effects: {
    * fetch({ payload }, { call, put }) {
      const response = yield call(queryMouthDetailsList, payload);
      const data = fetchValidation(response);
      yield data && put({
        type: 'save',
        payload: {
          ...data.body,
          cellCount: data.body.cellCount ? Object.entries(data.body.cellCount) : [],
        },
      });
    },

    * fetchCellType({ payload }, { call, put }) {
      const response = yield call(queryCellTypeInfo, payload);
      if (response && response.body) {
        const data = Object.entries(response.body).map((item: any) => ({
          key: item[0],
          value: item[1],
        }));
        yield put({
          type: 'saveCellType',
          payload: data,
        });
      }
    },

    * fetchStatus({ payload }, { call, put }) {
      const response = yield call(queryStatusInfo, payload);
      if (response && response.body) {
        const data = Object.entries(response.body).map((item: any) => ({
          key: item[0],
          value: item[1],
        }));
        yield put({
          type: 'saveState',
          payload: data,
        });
      }
    },

    * fetchDetail({ payload }, { call, put }) {
      const response = yield call(queryDetailInfo, payload);
      const data = fetchValidation(response);
      yield data && put({
        type: 'saveDetail',
        payload: data.body,
      });
    },

    * fetchLastInfo({ payload }, { call, put }) {
      const response = yield call(queryCollectionLastInfo, payload);
      const data = fetchValidation(response);
      yield data && put({
        type: 'saveLastInfo',
        payload: data.body,
      });
    },

    * checkMouth({ payload }, { call, put }) {
      if (payload.type === 'cell_updateCellIdle') { // 置空闲
        const { cellId } = payload;
        const query = { cellId };
        const response = yield call(operCheckCellOrder, query);
        if (response && !response.body) {
          yield put({ type: 'orderStatus', payload: response });
        } else yield put({ type: 'setMouth', payload });
      } else {
        yield put({ type: 'setMouth', payload });
      }
    },

    * setMouth({ payload: { search, ...query } }, { call, put }) {
      const response = yield call(operUpdateMouth, query);
      const data = fetchValidation(response);
      if (data) {
        message.success('设置成功');
        yield put({
          type: 'fetch',
          payload: search,
        });
      }
    },

    * changeDesc({ payload: { search, ...query } }, { call, put }) {
      const response = yield call(operUpdateDesc, query);
      const data = fetchValidation(response);
      if (data) {
        message.success('设置成功');
        yield put({ type: 'fetch', payload: search });
      }
    },

    * openCell({ payload: { search, ...query } }, { call, put }) {
      const response = yield call(operActionOpenCell, query);
      const data = fetchValidation(response);
      if (data?.code === 0) {
        message.success('打开成功');
        yield put({ type: 'fetch', payload: search });
      }
    },

    * addOrUpdateSet({ payload: { search, detailId, ...query } }, { call, put }) {
      const response = yield call(operactionSeting, query);
      const data = fetchValidation(response);
      if (data) {
        message.success(query.type === 1 ? '新增成功' : '删除成功');
        yield put({ type: 'fetch', payload: search });
        yield put({ type: 'fetchDetail', payload: { id: detailId } });
      }
    },

    *updateCellIdle({ payload: { search, cellIds } }, { call, put }) {
      const response = yield call(queryUpdateCellIdle, {cellIds});
      const data = fetchValidation(response);
      if (data) {
        message.success( '置空闲成功');
        yield put({ type: 'fetch', payload: search });
      }
    }
  },

  reducers: {
    save(state, action) {
      return {
        ...state,
        dataInfo: action.payload,
      };
    },

    orderStatus(state, { payload }) {
      return {
        ...state,
        unfinishedOrder: payload.msg,
        orderVisible: !payload.body,
      };
    },

    saveDetail(state, { payload }) {
      return {
        ...state,
        detailInfo: payload,
      };
    },

    saveLastInfo(state, { payload }) {
      return {
        ...state,
        lastInfo: payload,
      };
    },

    saveCellType(state, action) {
      return {
        ...state,
        cellTypeList: action.payload,
      };
    },

    saveState(state, action) {
      return {
        ...state,
        statusList: action.payload,
      };
    },
  },
};

export default Model;
